#include "main.h"

dockerType *dockerConfig = NULL;

/**
 * @brief Get the cpu info object
 *
 * @param cpu_info
 */
static void get_cpu_info(dev_cpu_info_t *cpu_info)
{

	char buf[1024] = {0};
	dev_cpu_info_t *cpu_occupy;
	cpu_occupy = cpu_info;

	FILE *fp = fopen("/proc/stat", "r");

	if (fp != NULL)
	{
		fgets(buf, sizeof(buf), fp);

		sscanf(buf, "%s %u %u %u %u %u %u %u", cpu_occupy->name, &cpu_occupy->user, &cpu_occupy->nice,
			   &cpu_occupy->system, &cpu_occupy->idle, &cpu_occupy->iowait, &cpu_occupy->irq, &cpu_occupy->softirq);

		fclose(fp);
	}
	else
	{
		printf("failed to open /proc/stat cpu info file.\n");
	}
}

/**
 * @brief cal_cpu_used_rate
 *
 * @param first
 * @param second
 * @return int
 */
static int cal_cpu_used_rate(dev_cpu_info_t *first, dev_cpu_info_t *second)
{
	unsigned int old_cpu_time, new_cpu_time;
	unsigned int usr_time_diff, sys_time_diff, nic_time_diff;
	int cpu_use = 0;

	old_cpu_time = (unsigned int)(first->user + first->nice + first->system + first->idle);
	new_cpu_time = (unsigned int)(second->user + second->nice + second->system + second->idle);

	usr_time_diff = (unsigned int)(second->user - first->user);
	sys_time_diff = (unsigned int)(second->system - first->system);
	nic_time_diff = (unsigned int)(second->nice - first->nice);

	if ((new_cpu_time - old_cpu_time) != 0)
		cpu_use = (int)100 * (usr_time_diff + sys_time_diff + nic_time_diff) / (new_cpu_time - old_cpu_time);
	else
		cpu_use = 0;

	return cpu_use;
}

/**
 * @brief Get the cpuUsage object
 *
 * @return uint8_t
 */
uint8_t get_cpuUsage(void)
{
	dev_cpu_info_t cpu_first, cpu_second;
	uint8_t cpu_rate = 0;

	get_cpu_info((dev_cpu_info_t *)&cpu_first);
	sleep(1);
	get_cpu_info((dev_cpu_info_t *)&cpu_second);

	/* calculate cpu usage rate */
	cpu_rate = cal_cpu_used_rate((dev_cpu_info_t *)&cpu_first, (dev_cpu_info_t *)&cpu_second);

	return cpu_rate;
}

/**
 * @brief cpu_scanDocker
 *
 * @return uint8_t
 */
uint8_t cpu_scanDocker(void)
{
	char buf[1024] = {0};

	char id[64] = {0};
	char name[64] = {0};
	char cpu[64] = {0};
	char mem[64] = {0};
	char block[64] = {0};

	char temp1[32] = {0};
	char temp2[32] = {0};
	char temp3[32] = {0};
	char temp4[32] = {0};
	char temp5[32] = {0};
	char temp6[32] = {0};
	// char *tmp = NULL;

	uint8_t docker_cnt = 0;

	FILE *fp = popen("docker stats --no-stream -a", "r");

	if (fp != NULL)
	{
		while (fgets(buf, sizeof(buf) - 1, fp) != 0)
		{
			// printf("buf:%s\n", buf);
			// 原有版本逻辑
			// sscanf(buf, "%s %s %s %s %s %s %s %s %s %s %s",
			// 	   id, name, cpu, tmp, tmp, tmp, mem, tmp, tmp, tmp, block);

			sscanf(buf, "%s %s %s %s %s %s %s %s %s %s %s",
				   id, name, cpu, temp1, temp2, temp3, mem, temp4, temp5, temp6, block);

			if (strcmp(id, "CONTAINER"))
			{
				// printf("id:%s, name:%s, cpu:%s, temp1:%s, temp2:%s, temp3:%s, mem:%s, temp4:%s, temp5:%s, temp6:%s, block:%s\n", id, name, cpu, temp1, temp2, temp3, mem, temp4, temp5, temp6, block);
				memcpy(dockerConfig[docker_cnt].name, name, 64);
				memcpy(dockerConfig[docker_cnt].cpu_usage, cpu, 64);
				memcpy(dockerConfig[docker_cnt].mem_usage, mem, 64);

				docker_cnt++;
			}
		}
	}
	else
	{
		printf("ERROR: CMD (docker stats --no-stream -a) error!\n");

		return 0;
	}

	pclose(fp);
	return docker_cnt;
}

/**
 * @brief Get the memUsage object
 *
 * @return uint8_t
 */
uint8_t get_memUsage(void)
{
	char buf[1024] = {0};

	char temp_name[64] = {0};
	char temp_memSize[64] = {0};
	char temp_unit[64] = {0};
	uint32_t MemTotal = 0;
	uint32_t MemFree = 0;

	uint8_t usage = 0;

	FILE *fp = fopen("/proc/meminfo", "r");

	if (fp != NULL)
	{
		while (fgets(buf, sizeof(buf) - 1, fp) != 0)
		{
			sscanf(buf, "%s %s %s", temp_name, temp_memSize, temp_unit);

			if (strcmp(temp_name, "MemTotal:") == 0)
			{
				MemTotal = atoi(temp_memSize);
			}
			else if (strcmp(temp_name, "MemFree:") == 0)
			{
				MemFree = atoi(temp_memSize);
			}
			else
			{
			}

			memset(temp_name, 0x00, sizeof(temp_name));
			memset(temp_memSize, 0x00, sizeof(temp_memSize));
			memset(temp_unit, 0x00, sizeof(temp_unit));

			memset(buf, 0x00, sizeof(buf));
		}

		if ((MemTotal != 0) && (MemFree != 0))
		{
			usage = 100 - (MemFree * 100 / MemTotal);
		}
	}
	else
	{
		printf("ERROR: failed to open /proc/meminfo info file.\n");
	}

	fclose(fp);

	return usage;
}

/**
 * @brief Get the flashUsage object
 *
 * @param result
 * @return uint8_t
 */
uint8_t get_flashUsage(char *result)
{
	char buf[1024] = {0};

	char Filesystem[64] = {0};
	char blocks[64] = {0};
	char Used[64] = {0};
	char Available[64] = {0};
	char Use[64] = {0};
	char Mounted[64] = {0};

	FILE *fp = popen("df /userdata", "r");

	if (fp != NULL)
	{
		while (fgets(buf, sizeof(buf) - 1, fp) != 0)
		{
			sscanf(buf, "%s %s %s %s %s %s",
				   Filesystem, blocks, Used, Available, Use, Mounted);

			if (strcmp(blocks, "1K-blocks"))
			{
				memcpy(result, Use, 64);
			}
		}
	}
	else
	{
		printf("ERROR: df /userdata error!\n");
		return FALSE;
	}

	return TRUE;
}
